﻿using System;
using Microsoft.Extensions.Configuration;

namespace Common
{
    /// <summary>
    /// 配置文件  获取处理类  明天后期可以添加加载配置文件的配置
    /// </summary>
    public  class Appsettings
    {
        private static IConfiguration _configuration;

        public  Appsettings(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        /// <summary>
        /// 获取配置文件
        /// </summary>
        /// <param name="sections"></param>
        /// <returns></returns>
        public static string app(params string[] sections)
        {
            if (sections.Length > 0)
            {
                // 如果没有读取到  那么就会返回null  这边一律返回空字符串
                return _configuration[string.Join(":", sections)]??"";
            }
            return "";
        }
        /// <summary>
        /// 获取配置文件  映射成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sections"></param>
        /// <returns></returns>
        public static T? app<T>(params string[] sections) where T:class
        {
            if (sections.Length==0)
            {
                throw new ArgumentException(nameof(sections));
            }
            return _configuration.GetSection(string.Join(":", sections)).Get<T>();
        }
        

    }
}
